import os #line:1:import os
import sys #line:2:import sys
import json #line:3:import json
import requests #line:4:import requests
from argparse import ArgumentParser #line:6:from argparse import ArgumentParser
try :#line:8:try:
    from utils import crypto #line:9:from utils import crypto
    from utils import data #line:10:from utils import data
    from utils .logger import Logger #line:11:from utils.logger import Logger
    from utils .request import Request #line:12:from utils.request import Request
except ImportError :#line:13:except ImportError:
    from realme_ota .utils import crypto #line:14:from realme_ota.utils import crypto
    from realme_ota .utils import data #line:15:from realme_ota.utils import data
    from realme_ota .utils .request import Request #line:16:from realme_ota.utils.request import Request
    from realme_ota .utils .logger import Logger #line:17:from realme_ota.utils.logger import Logger
def main ():#line:19:def main():
    OOOO0O00OO0O0O0O0 =ArgumentParser ()#line:20:parser = ArgumentParser()
    O0O0OOOOOO0O000OO =OOOO0O00OO0O0O0O0 .add_mutually_exclusive_group ()#line:22:verbosity = parser.add_mutually_exclusive_group()
    O0O0OOOOOO0O000OO .add_argument ("-v","--verbosity",type =int ,choices =range (0 ,6 ),default =4 ,help ="Set the verbosity level. Range: 0 (no logging) to 5 (debug). Default: 4 (info).")#line:23:verbosity.add_argument("-v", "--verbosity", type=int, choices=range(0, 6), default=4, help="Set the verbosity level. Range: 0 (no logging) to 5 (debug). Default: 4 (info).")
    O0O0OOOOOO0O000OO .add_argument ("-s","--silent",action ='store_true',help ="Enable silent output (purge logging). Shortcut for '-v0'.")#line:24:verbosity.add_argument("-s", "--silent", action='store_true', help="Enable silent output (purge logging). Shortcut for '-v0'.")
    OOOO0O00OO0O0O0O0 .add_argument ("product_model",type =str ,help ="Product Model (ro.product.name).")#line:26:parser.add_argument("product_model", type=str, help="Product Model (ro.product.name).")
    OOOO0O00OO0O0O0O0 .add_argument ("ota_version",help ="OTA Version (ro.build.version.ota).")#line:27:parser.add_argument("ota_version", help="OTA Version (ro.build.version.ota).")
    OOOO0O00OO0O0O0O0 .add_argument ("rui_version",type =int ,choices =[1 ,2 ,3 ,4 ,5 ,6 ],help ="RealmeUI Version (ro.build.version.realmeui).")#line:28:parser.add_argument("rui_version", type=int, choices=[1, 2, 3, 4, 5, 6], help="RealmeUI Version (ro.build.version.realmeui).")
    OOOO0O00OO0O0O0O0 .add_argument ("nv_identifier",type =str ,nargs ='?',help ="NV (carrier) identifier (ro.build.oplus_nv_id) (if none, provide 0 or omit).")#line:29:parser.add_argument("nv_identifier", type=str, nargs='?', help="NV (carrier) identifier (ro.build.oplus_nv_id) (if none, provide 0 or omit).")
    OOOO0OOO000OO00OO =OOOO0O00OO0O0O0O0 .add_argument_group ("request options")#line:31:req_opts = parser.add_argument_group("request options")
    OOOO0OOO000OO00OO .add_argument ("-r","--region",type =int ,choices =[0 ,1 ,2 ,3 ],default =0 ,help ="Use custom region for the request (GL = 0, CN = 1, IN = 2, EU = 3).")#line:32:req_opts.add_argument("-r", "--region", type=int, choices=[0, 1, 2, 3], default=0, help="Use custom region for the request (GL = 0, CN = 1, IN = 2, EU = 3).")
    OOOO0OOO000OO00OO .add_argument ("-g","--guid",type =str ,default ="0",help ="The guid of the third line in the file /data/system/openid_config.xml (only required to extract 'CBT' in China).")#line:33:req_opts.add_argument("-g", "--guid", type=str, default="0", help="The guid of the third line in the file /data/system/openid_config.xml (only required to extract 'CBT' in China).")
    OOOO0OOO000OO00OO .add_argument ("-i","--imei",type =str ,nargs ='+',help ="Specify one or two IMEIs for the request.")#line:34:req_opts.add_argument("-i", "--imei", type=str, nargs='+', help="Specify one or two IMEIs for the request.")
    OOOO0OOO000OO00OO .add_argument ("-b","--beta",action ='store_true',help ="Try to get a test version (IMEI probably required).")#line:35:req_opts.add_argument("-b", "--beta", action='store_true', help="Try to get a test version (IMEI probably required).")
    OOOO0OOO000OO00OO .add_argument ("-l","--language",type =str ,default =None ,help ="Specify the language of response (en-EN by default, zh-CN in China).")#line:36:req_opts.add_argument("-l", "--language", type=str, default=None, help="Specify the language of response (en-EN by default, zh-CN in China).")
    OOOO0OOO000OO00OO .add_argument ("--old-method",action ='store_true',help ="Use old method for the request (only applies if rui_version >= 2).")#line:37:req_opts.add_argument("--old-method", action='store_true', help="Use old method for the request (only applies if rui_version >= 2).")
    OO00O0OO000O0O00O =OOOO0O00OO0O0O0O0 .add_argument_group ("output options")#line:39:out_opts = parser.add_argument_group("output options")
    OO00O0OO000O0O00O .add_argument ("-d","--dump",type =str ,help ="Save request response into a file.")#line:40:out_opts.add_argument("-d", "--dump", type=str, help="Save request response into a file.")
    OO00O0OO000O0O00O .add_argument ("-o","--only",type =str ,help ="Only show the desired value from the response.")#line:41:out_opts.add_argument("-o", "--only", type=str, help="Only show the desired value from the response.")
    O00OO0O0OO0OO000O =OOOO0O00OO0O0O0O0 .parse_args ()#line:43:args = parser.parse_args()
    OOO0OOO00O0O000O0 =Logger (level =0 if O00OO0O0OO0OO000O .silent else O00OO0O0OO0OO000O .verbosity )#line:47:)
    O0000OO000OOO0OO0 =Request (req_version =1 if (O00OO0O0OO0OO000O .old_method or O00OO0O0OO0OO000O .rui_version ==1 )else 2 ,model =O00OO0O0OO0OO000O .product_model ,ota_version =O00OO0O0OO0OO000O .ota_version ,rui_version =O00OO0O0OO0OO000O .rui_version ,nv_identifier =O00OO0O0OO0OO000O .nv_identifier ,region =O00OO0O0OO0OO000O .region ,deviceId =O00OO0O0OO0OO000O .guid ,imei0 =O00OO0O0OO0OO000O .imei [0 ]if O00OO0O0OO0OO000O .imei and len (O00OO0O0OO0OO000O .imei )>0 else None ,imei1 =O00OO0O0OO0OO000O .imei [1 ]if O00OO0O0OO0OO000O .imei and len (O00OO0O0OO0OO000O .imei )>1 else None ,beta =O00OO0O0OO0OO000O .beta ,language =O00OO0O0OO0OO000O .language )#line:61:)
    OOO0OOO00O0O000O0 .log (f"Load payload for {O00OO0O0OO0OO000O.product_model} (RealmeUI V{O00OO0O0OO0OO000O.rui_version})")#line:63:logger.log(f"Load payload for {args.product_model} (RealmeUI V{args.rui_version})")
    try :#line:64:try:
        O0000OO000OOO0OO0 .set_vars ()#line:65:request.set_vars()
        OOO0O0OOOO0O0O0OO ,OOOOOO0OO0O000O0O ,O0O0OOO0OO00OO000 =O0000OO000OOO0OO0 .set_body_headers ()#line:66:req_body, req_hdrs, plain_body = request.set_body_headers()
    except Exception as O0O0OOOO0O00OOO00 :#line:67:except Exception as e:
        OOO0OOO00O0O000O0 .die (f"Something went wrong while setting the request variables :( ({O0O0OOOO0O00OOO00})!",2 )#line:68:logger.die(f"Something went wrong while setting the request variables :( ({e})!", 2)
    OOO0OOO00O0O000O0 .log (f"Request headers:\n{json.dumps(OOOOOO0OO0O000O0O, indent=4, sort_keys=True, ensure_ascii=False)}",5 )#line:70:logger.log(f"Request headers:\n{json.dumps(req_hdrs, indent=4, sort_keys=True, ensure_ascii=False)}", 5)
    OOO0OOO00O0O000O0 .log (f"Request body:\n{json.dumps(O0O0OOO0OO00OO000, indent=4, sort_keys=True, ensure_ascii=False)}",5 )#line:71:logger.log(f"Request body:\n{json.dumps(plain_body, indent=4, sort_keys=True, ensure_ascii=False)}", 5)
    OOO0OOO00O0O000O0 .log (f"Encrypted body:\n{json.dumps(OOO0O0OOOO0O0O0OO, indent=4, sort_keys=True, ensure_ascii=False)}",5 )#line:72:logger.log(f"Encrypted body:\n{json.dumps(req_body, indent=4, sort_keys=True, ensure_ascii=False)}", 5)
    OOO0OOO00O0O000O0 .log ("Wait for the endpoint to reply")#line:74:logger.log("Wait for the endpoint to reply")
    try :#line:75:try:
        OOOO00OOOOOO00OOO =requests .post (O0000OO000OOO0OO0 .url ,data =O0000OO000OOO0OO0 .body ,headers =O0000OO000OOO0OO0 .headers ,timeout =30 )#line:76:response = requests.post(request.url, data = request.body, headers = request.headers, timeout = 30)
    except Exception as O0O0OOOO0O00OOO00 :#line:77:except Exception as e:
        OOO0OOO00O0O000O0 .die (f"Something went wrong while requesting to the endpoint :( {O0O0OOOO0O00OOO00}!",1 )#line:78:logger.die(f"Something went wrong while requesting to the endpoint :( {e}!", 1)
    try :#line:80:try:
        O0000OO000OOO0OO0 .validate_response (OOOO00OOOOOO00OOO )#line:81:request.validate_response(response)
    except Exception as O0O0OOOO0O00OOO00 :#line:82:except Exception as e:
        if O00OO0O0OO0OO000O .ota_version [-17 :]!='0000_000000000000':#line:83:if args.ota_version[-17:] != '0000_000000000000':
            sys .argv [sys .argv .index (O00OO0O0OO0OO000O .ota_version )]=O00OO0O0OO0OO000O .ota_version [:-17 ]+'0000_000000000000'#line:84:sys.argv[sys.argv.index(args.ota_version)] = args.ota_version[:-17] + '0000_000000000000'
            os .execl (sys .executable ,sys .executable ,*sys .argv )#line:85:os.execl(sys.executable, sys.executable, *sys.argv)
        OOO0OOO00O0O000O0 .die (f'{O0O0OOOO0O00OOO00}',1 )#line:86:logger.die(f'{e}', 1)
    else :#line:87:else:
        OOO0OOO00O0O000O0 .log ("All good")#line:88:logger.log("All good")
    OOO0OOO00O0O000O0 .log ("Let's rock")#line:90:logger.log("Let's rock")
    try :#line:91:try:
        OO0O00O00O0O0OOO0 =json .loads (OOOO00OOOOOO00OOO .content )#line:92:json_response = json.loads(response.content)
        OOO0OOO00O0O000O0 .log (f"Response:\n{json.dumps(OO0O00O00O0O0OOO0, indent=4, sort_keys=True, ensure_ascii=False)}",5 )#line:93:logger.log(f"Response:\n{json.dumps(json_response, indent=4, sort_keys=True, ensure_ascii=False)}", 5)
        O00OOO000OOOO0OOO =json .loads (O0000OO000OOO0OO0 .decrypt (OO0O00O00O0O0OOO0 [O0000OO000OOO0OO0 .resp_key ]))#line:94:content = json.loads(request.decrypt(json_response[request.resp_key]))
    except Exception as O0O0OOOO0O00OOO00 :#line:95:except Exception as e:
        OOO0OOO00O0O000O0 .die (f"解析响应失败: {O0O0OOOO0O00OOO00}",2 )#line:96:logger.die(f"解析响应失败: {e}", 2)
    OOOO000000OO0O0O0 ={"colorOSVersion":O00OOO000OOOO0OOO .get ("colorOSVersion",""),"ota_target_version":O00OOO000OOOO0OOO .get ("components",[{}])[0 ].get ("componentPackets",{}).get ("vabInfo",{}).get ("ota_target_version",""),"url":O00OOO000OOOO0OOO .get ("components",[{}])[0 ].get ("componentPackets",{}).get ("url",""),"versionName":O00OOO000OOOO0OOO .get ("versionName","")}#line:104:}
    OO00O0O00O00OOOO0 ={OOO00000O0OOOOOO0 :OOO0OOO0OOOO00O0O for OOO00000O0OOOOOO0 ,OOO0OOO0OOOO00O0O in OOOO000000OO0O0O0 .items ()if OOO0OOO0OOOO00O0O }#line:106:filtered_output = {k: v for k, v in output.items() if v}
    print (json .dumps (OO00O0O00O00OOOO0 ,indent =4 ,ensure_ascii =False ))#line:107:print(json.dumps(filtered_output, indent=4, ensure_ascii=False))
if __name__ =='__main__':#line:108:if __name__ == '__main__':
    main ()#line:109:main()
